package com.googlecode.pacswitch;

import java.io.ByteArrayOutputStream;

import javax.xml.bind.JAXB;

/**
 * @author Andrey Pavlenko
 */
public class Utils {

    public static int ipToInt(final String ip) {
        final String[] values = ip.split("\\.");
        return (Integer.parseInt(values[0]) << 24)
                + (Integer.parseInt(values[1]) << 16)
                + (Integer.parseInt(values[2]) << 8)
                + (Integer.parseInt(values[3]));

    }

    public static String intToIp(final int ip) {
        return Integer.toString((ip >>> 24) & 0xFF) + '.'
                + Integer.toString((ip >>> 16) & 0xFF) + '.'
                + Integer.toString((ip >>> 8) & 0xFF) + '.'
                + Integer.toString(ip & 0xFF);
    }

    public static String marshall(final Object jaxbObject) {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        JAXB.marshal(jaxbObject, baos);
        return baos.toString();
    }
}
